package com.hlx.service.user.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hlx.entity.user.Ticket;
import com.hlx.mapper.user.TicketMapper;
import com.hlx.service.user.TicketService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TicketServiceImpl extends ServiceImpl<TicketMapper, Ticket> implements TicketService {
    @Override
    public Integer getIdByTicketName(Integer trainId, Integer personId, LocalDate date) {
        LambdaQueryWrapper<Ticket> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Ticket::getTrainId,trainId);
        queryWrapper.in(Ticket::getDate,date);
        queryWrapper.in(Ticket::getPersonId,personId);

        Integer id = 0;
        id = this.getOne(queryWrapper).getId();
        return id;
    }

    @Override
    public List<Ticket> getTicketByPersonId(Integer personId){
        LambdaQueryWrapper<Ticket> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Ticket::getPersonId,personId);
        return this.list(queryWrapper);
    }

//    @Override
//    public List<Ticket> getTicketByCustomerId(List<Integer> customerId) {
//        Integer[] status = {0,1};
//        LambdaQueryWrapper<Ticket> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.in(Ticket::getPersonId, customerId);
//        queryWrapper.and((wrapper)->wrapper.in(Ticket::getStatus,status));
//        return this.list(queryWrapper);
//    }

    @Override
    public Page getTicketByCustomerId(List<Integer> customerId,int currentPage,int pageSize) {
//        分页构造
        Page page = new Page(currentPage,pageSize);
//        查询条件构造
        Integer[] status = {0,1};
        LambdaQueryWrapper<Ticket> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Ticket::getPersonId, customerId);
        queryWrapper.and((wrapper)->wrapper.in(Ticket::getStatus,status));
        return this.page(page,queryWrapper);
    }

    @Override
    public boolean deleteTicketByTrainId(Integer trainId) {
        LambdaQueryWrapper<Ticket> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Ticket::getTrainId,trainId);
        return this.remove(queryWrapper);
    }

    @Override
    public List<Integer> getAllTicketIdByTrainNumberId(Integer trainId) {
        LambdaQueryWrapper<Ticket> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Ticket::getTrainId,trainId);
        return this.list(queryWrapper).stream().map(Ticket::getId).collect(Collectors.toList());
    }
}
